package miiteen.customsportal.data.service;


import miiteen.customsportal.data.beans.DataInfoImage;
import miiteen.customsportal.data.dao.DataInfoImageDao;
import miiteen.hr.pubconfig.CurrentHrInfo;
import miiteen.hr.user.beans.UserUserInfo;
import miiteen.sys.login.bean.UserShiro;
import miiteen.sys.login.utils.LoginUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import miiteen.data.dao.BaseDao;
import miiteen.data.service.impl.BaseServiceImpl;

import miiteen.customsportal.data.beans.DataInfo;
import miiteen.customsportal.data.dao.DataInfoDao;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Name: CustomsPortal_Data_Info
 * Des: -Service
 * Author: yulp
 * Date: 2020-07-10
 */
@Service("dataInfoService")
public class DataInfoService extends BaseServiceImpl<DataInfo> {

    @Autowired
    private DataInfoDao dataInfoDao;

    @Autowired
    private DataInfoImageDao dataInfoImageDao;

    @Override
    protected BaseDao<DataInfo> getDao() {
        return dataInfoDao;
    }


    @Transactional
    public long save(DataInfo dataInfo) {
        UserShiro.ShiroUser shiroUser = LoginUtil.getCurUser();
        UserUserInfo userInfo = CurrentHrInfo.getUserInfo(shiroUser);

        if (dataInfo.getId() > 0) {
            //修改
            dataInfo.setUpdateId(userInfo.getId());
            dataInfo.setUpdateTime(new Date());
            dataInfo.setUpdator(userInfo.getTrueName());
            dataInfo.setDeptId(userInfo.getDeptId());
            //保存
        } else {

            dataInfo.setCreateId(userInfo.getId());
            dataInfo.setCreateTime(new Date());
            dataInfo.setCreator(userInfo.getTrueName());
            dataInfo.setDeptId(userInfo.getDeptId());
        }

        long result = dataInfoDao.save(dataInfo);
        if(result>0){
//            删除子表数据
            dataInfoImageDao.delete(String.valueOf(dataInfo.getId()));
            for(DataInfoImage dataInfoImage:dataInfo.getList()){
                dataInfoImage.setDataId(dataInfo.getId());
                if(dataInfoImageDao.save(dataInfoImage)<0){
                    throw new RuntimeException("保存失败");
                }
            }
        }
        return result;
    }
    @Transactional
    public int delete(String ids){

        int result=dataInfoDao.delete(ids);

        dataInfoImageDao.delete(ids);

        return result;
    }

    public List<DataInfo> findAllDept(){
        return dataInfoDao.findAllDept();
    }
    public DataInfo findListByMapPreviousNext(Map<String,Object> map){
        return dataInfoDao.findListByMapPreviousNext(map);
    }

    public  List<DataInfo> findAllCategory(Map<String, Object> map){
        return dataInfoDao.findAllCategory(map);
    }
    public  List<DataInfo> findByCategory(Map<String, Object> map){
        return dataInfoDao.findByCategory(map);
    }
    public  DataInfo findPreviousNextCategory(Map<String, Object> map){
        return dataInfoDao.findPreviousNextCategory(map);
    }
}
